package com.googlecode.openauth.web.action;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;

import com.googlecode.openauth.web.config.CommonProperty;
import com.googlecode.openauth.web.dao.IAreaDao;
import com.googlecode.openauth.web.dao.IAreaLevelDao;
import com.googlecode.openauth.web.dao.IRelationDao;
import com.googlecode.openauth.web.dao.IRoleAreaDao;
import com.googlecode.openauth.web.dao.IRoleDao;
import com.googlecode.openauth.web.entity.ActionPageBean;
import com.googlecode.openauth.web.entity.AreaInfo;
import com.googlecode.openauth.web.entity.RoleInfo;
import com.googlecode.openauth.web.entity.UserInfo;
import com.googlecode.openauth.web.service.IAreaService;
import com.googlecode.openauth.web.util.Util;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AreaAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7008194480212163148L;
	
	private IAreaDao areaDao;
	private IRoleDao roleDao;
	private IRoleAreaDao roleAreaDao;
	private IRelationDao relationDao;
	private List<AreaInfo> list;
	private IAreaLevelDao areaLevelDao;
	private List<AreaInfo> levels;
	private String[] parentArea;
	private int areaId;
	private int[] areaIds;
	private String areaName;
	private String uuid;
	private String areaDesc;
	private int levelId;
	private String levelName;
	private IAreaService service;
	private String levelDesc;
	private int areaParent;

	private ActionPageBean<AreaInfo> actionPageBean;
	private int currentPageNo = 1;
	private int prevPageNo = 1;
	private int nextPageNo = 1;
	private int totalCount = 0;
	private int pageNum = CommonProperty.PAGENUM;
	
	@Override
	public String execute() throws Exception {
		list = service.getAreas(0);
		actionPageBean = new ActionPageBean<AreaInfo>();
		actionPageBean.setData(list);
		return SUCCESS;
	}

	private UserInfo getCurUser(){
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		Object obj = session.get("user");
		UserInfo result = new UserInfo();
		result.setUserId(-1);
		if(obj == null){
			return result;
		}
		
		if(!(obj instanceof UserInfo)){
			return result;
		}
		
		result = (UserInfo) obj;
		return result;
	}
	
	private RoleInfo getCurRole(){
		RoleInfo result = new RoleInfo();
		result.setRoleId(-1);
		
		UserInfo userInfo = getCurUser();
		List<RoleInfo> roles = userInfo.getRoleInfos();
		
		if(roles == null || roles.isEmpty()){
			return result;
		}
		
		result = roles.get(0);
		return result;
	}
	
	private int getCurRoleId() {
		RoleInfo role = getCurRole();
		int curRoleId = role.getRoleId();
		return curRoleId;
	}
	
	public String getFirstPage() throws Exception{
		RoleInfo curRole = getCurRole();
		int totalCount = areaDao.getAllAreasCount(curRole);
		actionPageBean = new ActionPageBean<AreaInfo>();
		actionPageBean.setTotalCount(totalCount);
		
		ActionPageBean<AreaInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		pageBean.setPrevPageNo(1);
		pageBean.setCurrentPageNo(1);
		Util util = new Util();
		int maxPageNo = util.getMaxPageNo(pageBean);
		if(maxPageNo <= 1){
			pageBean.setNextPageNo(1);
		}else{
			pageBean.setNextPageNo(2);
		}
		
		int offset = 0;
		int limit = pageBean.getPageNum();
		RowBounds rowBounds = new RowBounds(offset, limit);
		setPageBean(pageBean);
		List<AreaInfo> data = areaDao.getAllAreas(curRole,rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}
	
	public String getPrevPage() throws Exception{
		RoleInfo curRole = getCurRole();
		int totalCount = areaDao.getAllAreasCount(curRole);
		actionPageBean = new ActionPageBean<AreaInfo>();
		actionPageBean.setTotalCount(totalCount);
		
		ActionPageBean<AreaInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		RowBounds rowBounds = util.calcPrevRowBounds(pageBean);
		setPageBean(pageBean);
		List<AreaInfo> data = areaDao.getAllAreas(curRole,rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}
	
	public String getNextPage() throws Exception{
		RoleInfo curRole = getCurRole();
		int totalCount = areaDao.getAllAreasCount(curRole);
		actionPageBean = new ActionPageBean<AreaInfo>();
		actionPageBean.setTotalCount(totalCount);
		
		ActionPageBean<AreaInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		setPageBean(pageBean);
		List<AreaInfo> data = areaDao.getAllAreas(curRole,rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}
	
	public String getLastPage() throws Exception{
		RoleInfo curRole = getCurRole();
		int totalCount = areaDao.getAllAreasCount(curRole);
		actionPageBean = new ActionPageBean<AreaInfo>();
		actionPageBean.setTotalCount(totalCount);
		
		ActionPageBean<AreaInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		int maxPageNo = util.getMaxPageNo(pageBean);
		pageBean.setCurrentPageNo(maxPageNo);
		pageBean.setNextPageNo(maxPageNo);

		if(maxPageNo <= 1){
			pageBean.setPrevPageNo(1);
		}else{
			pageBean.setPrevPageNo(maxPageNo - 1);
		}
		int limit = pageBean.getPageNum();
		int offset = (maxPageNo - 1) * limit;
		RowBounds rowBounds = new RowBounds(offset, limit);
		setPageBean(pageBean);
		List<AreaInfo> data = areaDao.getAllAreas(curRole,rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}
	
	public String init() throws Exception {
		//list = areaDao.getAreaActionByAreaId(0);
		RoleInfo curRole = getCurRole();
		list = areaDao.getAllAreas(curRole, new RowBounds(0, pageNum));
		levels = areaLevelDao.getAreaLevels();
		return SUCCESS;
	}

	public String initUPdate() throws Exception {
		AreaInfo area = areaDao.getAreaLevelByid(areaId);
		setFrom(area);
		return SUCCESS;
	}

	public String update() throws Exception {
		AreaInfo bean = new AreaInfo();
		bean.setAreaDesc(areaDesc);
		bean.setAreaName(areaName);
		bean.setLevelId(levelId);
		bean.setParentId(areaParent);
		bean.setAreaId(areaId);
		areaDao.updateAreaAction(bean);
		return SUCCESS;
	}

	private void setFrom(AreaInfo area) {
		this.areaDesc = area.getAreaDesc();
		this.areaId = area.getAreaId();
		this.areaName = area.getAreaName();
		this.levelId = area.getLevelId();
		this.uuid = area.getUuid();
		this.areaParent = area.getParentId();
	}

	public String save() throws Exception {
		list = areaDao.getAreaActionByAreaId(0);
		levels = areaLevelDao.getAreaLevels();
		String parentId = "";
		for (String str : parentArea) {
			System.out.println(str);
			if (!str.equals("-1")) {
				parentId = str;
			} else {
				break;
			}
		}
		int levId = 2;
		AreaInfo bean = new AreaInfo();
		bean.setAreaDesc(areaDesc);
		bean.setAreaName(areaName);
		if (!parentId.isEmpty()) {
			levId = areaDao.getAreaLevelByid(Integer.parseInt(parentId))
					.getLevelId() + 1;
			bean.setParentId(Integer.parseInt(parentId));
		}else{
			//parent为国家
			bean.setParentId(1);
		}
		bean.setLevelId(levId);
		areaDao.addAreaAction(bean);
		 
		return SUCCESS;
	}

	public String delete() throws Exception {

		areaDao.delAreaActionByAreaId(areaIds[0]);
		return SUCCESS;
	}

	public int[] getAreaIds() {
		return areaIds;
	}

	public void setAreaIds(int[] areaIds) {
		this.areaIds = areaIds;
	}

	public IAreaDao getAreaDao() {
		return areaDao;
	}

	public void setAreaDao(IAreaDao areaDao) {
		this.areaDao = areaDao;
	}

	public List<AreaInfo> getList() {
		return list;
	}

	public void setList(List<AreaInfo> list) {
		this.list = list;
	}

	public IAreaLevelDao getAreaLevelDao() {
		return areaLevelDao;
	}

	public void setAreaLevelDao(IAreaLevelDao areaLevelDao) {
		this.areaLevelDao = areaLevelDao;
	}

	public List<AreaInfo> getLevels() {
		return levels;
	}

	public void setLevels(List<AreaInfo> levels) {
		this.levels = levels;
	}

	public String[] getParentArea() {
		return parentArea;
	}

	public void setParentArea(String[] parentArea) {
		this.parentArea = parentArea;
	}

	public int getAreaId() {
		return areaId;
	}

	public void setAreaId(int areaId) {
		this.areaId = areaId;
	}

	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getAreaDesc() {
		return areaDesc;
	}

	public void setAreaDesc(String areaDesc) {
		this.areaDesc = areaDesc;
	}

	public int getLevelId() {
		return levelId;
	}

	public void setLevelId(int levelId) {
		this.levelId = levelId;
	}

	public String getLevelName() {
		return levelName;
	}

	public void setLevelName(String levelName) {
		this.levelName = levelName;
	}

	public String getLevelDesc() {
		return levelDesc;
	}

	public void setLevelDesc(String levelDesc) {
		this.levelDesc = levelDesc;
	}

	public IAreaService getService() {
		return service;
	}

	public void setService(IAreaService service) {
		this.service = service;
	}

	public int getAreaParent() {
		return areaParent;
	}

	public void setAreaParent(int areaParent) {
		this.areaParent = areaParent;
	}

	//分页用到的实体
	public ActionPageBean<AreaInfo> getActionPageBean() {
		return actionPageBean;
	}

	public void setActionPageBean(ActionPageBean<AreaInfo> actionPageBean) {
		this.actionPageBean = actionPageBean;
	}
	
	public ActionPageBean<AreaInfo> getPageBean(){
		ActionPageBean<AreaInfo> bean = new ActionPageBean<AreaInfo>();
		bean.setCurrentPageNo(currentPageNo);
		bean.setPrevPageNo(prevPageNo);
		bean.setNextPageNo(nextPageNo);
		bean.setTotalCount(totalCount);
		bean.setPageNum(pageNum);
		return bean;
	}
	
	public void setPageBean(ActionPageBean<AreaInfo> bean){
		currentPageNo = bean.getCurrentPageNo();
		prevPageNo = bean.getPrevPageNo();
		nextPageNo = bean.getNextPageNo();
		totalCount = bean.getTotalCount();
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public int getPrevPageNo() {
		return prevPageNo;
	}

	public void setPrevPageNo(int prevPageNo) {
		this.prevPageNo = prevPageNo;
	}

	public int getNextPageNo() {
		return nextPageNo;
	}

	public void setNextPageNo(int nextPageNo) {
		this.nextPageNo = nextPageNo;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public IRoleAreaDao getRoleAreaDao() {
		return roleAreaDao;
	}

	public void setRoleAreaDao(IRoleAreaDao roleAreaDao) {
		this.roleAreaDao = roleAreaDao;
	}

	public IRelationDao getRelationDao() {
		return relationDao;
	}

	public void setRelationDao(IRelationDao relationDao) {
		this.relationDao = relationDao;
	}

}
